using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpcuaClient.Model;

namespace OpcuaClient.View
{
    public partial class Form_CollectionServerConfig : Form
    {
        public bool Status { get; set; }
        public CollectServer Server { get; set; }
        public Form_CollectionServerConfig()
        {
            InitializeComponent();
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            Status = false;
            Close();
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            InitServer();
            if (!Status) return;
            Close();
        }

        private void InitServer()
        {
            if (Server == null)
            {
                Server = new CollectServer();
            }
            if (string.IsNullOrWhiteSpace(tbName.Text) || string.IsNullOrWhiteSpace(tbCode.Text)
                || string.IsNullOrWhiteSpace(tbUrl.Text) || string.IsNullOrWhiteSpace(tbFrequency.Text)
                || string.IsNullOrWhiteSpace(cbMessageType.Text) || string.IsNullOrWhiteSpace(tbMessageUrl.Text))
            {
                MessageBox.Show(this, "信息配置不完整，请检查!", "错误提示！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            Server.Name = tbName.Text;
            Server.Code = tbCode.Text;
            Server.Url = tbUrl.Text;
            Server.UserName = tbUserName.Text;
            Server.Password = tbPassword.Text;
            Server.Frequency = int.Parse(tbFrequency.Text);
            Server.MessageType = cbMessageType.Text;
            Server.MessageUrl = tbMessageUrl.Text;
            Server.MessageUser = tbMessageUser.Text;
            Server.MessagePwd = tbMessagePwd.Text;
            Status = true;
        }

        public void Config()
        {
            if (Server == null)
            {
                Server = new CollectServer();
                return;
            }
            tbName.Text = Server.Name;
            tbCode.Text = Server.Code;
            tbUrl.Text = Server.Url;
            tbUserName.Text = Server.UserName;
            tbPassword.Text = Server.Password;
            tbFrequency.Text = Server.Frequency.ToString();
            cbMessageType.Text = Server.MessageType;
            tbMessageUrl.Text = Server.MessageUrl;
            tbMessageUser.Text = Server.MessageUser;
            tbMessagePwd.Text = Server.MessagePwd;
            Status = true;
        }
    }
}
